﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.DirectX.Direct3D;
using RPGOnline.Controls.Map.Events;
using RPGOnline.Controls.Map.Layers;
using RPGOnline.Properties;
using RPGOnline.Rendering;
using WeifenLuo.WinFormsUI.Docking;

namespace RPGOnline.Controls.Map
{
    public partial class MapControl : DockContent
    {
        private MapControlPresenter m_presenter;

        public event EventHandler<CellEventArgs> OnCellMouseDown;
        public event EventHandler<CellEventArgs> OnCellMouseMove;
        public event EventHandler<CellEventArgs> OnCellMouseUp;

        public MapControl()
        {
            InitializeComponent();

            mapDrawerControl1.OnCellMouseMove += InvokeOnCellMouseMove;
        }

        public MapLayer OverlayLayer { get { return mapDrawerControl1.OverlayLayer; } }
        public Map Map { get { return mapDrawerControl1.Map; } }

    	public bool GridVisible
    	{
			get { return mapDrawerControl1.GridVisible; }
			set { mapDrawerControl1.GridVisible = value; }
    	}

        public Renderer Device
        {
            get { return mapDrawerControl1.Renderer; }
        }

        private void InvokeOnCellMouseMove(object sender, CellEventArgs e)
        {
            if (OnCellMouseMove != null)
                OnCellMouseMove(this, e);
        }

        public void SetPresenter(MapControlPresenter presenter)
        {
            m_presenter = presenter;
        }

        private void tsbNewMap_Click(object sender, EventArgs e)
        {
            m_presenter.CreateNewMap();
        }

        public void SetCurrentMap(Map map)
        {
            mapDrawerControl1.Map = map;
            mapDrawerControl1.Invalidate();

            tscLayers.Items.AddRange(map.Layers.ToArray());
            tscLayers.SelectedItem = map.CurrentLayer;
        }

        private void mapDrawerControl1_OnCellMouseDown(object sender, CellEventArgs e)
        {
            if (OnCellMouseDown != null)
                OnCellMouseDown(this, e);
        }

        private void mapDrawerControl1_OnCellMouseMove(object sender, CellEventArgs e)
        {
            if (OnCellMouseMove != null)
                OnCellMouseMove(this, e);
        }

        private void mapDrawerControl1_OnCellMouseUp(object sender, CellEventArgs e)
        {
            if (OnCellMouseUp != null)
                OnCellMouseUp(this, e);
        }

        private void tsbPaint_Click(object sender, EventArgs e)
        {
			if (tsbPaint.Checked)
			{
				tsbAreaPaint.Checked = false;
				tsbAddLight.Checked = false;
				tsbAddWall.Checked = false;

				m_presenter.EnablePaint();
			}
        }

        private void tscLayers_SelectedIndexChanged(object sender, EventArgs e)
        {
            m_presenter.SetCurrentLayer(tscLayers.SelectedItem as MapLayer);
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            m_presenter.CreateNewLayer("<New>");
        }

        public void SetCurrentLayer(MapLayer mapLayer)
        {
            tscLayers.Items.Clear();
            tscLayers.Items.AddRange(mapDrawerControl1.Map.Layers.ToArray());
            tscLayers.SelectedItem = mapLayer;
        }

        private void tsbAreaPaint_Click(object sender, EventArgs e)
        {
			if (tsbAreaPaint.Checked)
			{
				tsbPaint.Checked = false;
				tsbAddLight.Checked = false;
				tsbAddWall.Checked = false;

				m_presenter.OnAreaPaint();
			}
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            m_presenter.ShowLights(tsbShowLights.Checked);
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            m_presenter.ShowWalls(tsbShowWalls.Checked);
        }

		private void tsbShowGrid_CheckStateChanged(object sender, EventArgs e)
		{
			m_presenter.ShowGrid(tsbShowGrid.Checked);
		}

		private void tsbAddLight_Click(object sender, EventArgs e)
		{
			if (tsbAddLight.Checked)
			{
				tsbPaint.Checked = false;
				tsbAreaPaint.Checked = false;
				tsbAddWall.Checked = false;

				m_presenter.OnAddLight();
			}
		}

		private void tsbAddWall_Click(object sender, EventArgs e)
		{
			if (tsbAddWall.Checked)
			{
				tsbPaint.Checked = false;
				tsbAreaPaint.Checked = false;
				tsbAddLight.Checked = false;

				m_presenter.OnAddWall();
			}
		}

        private void tsbRemoveWall_Click(object sender, EventArgs e)
        {
            m_presenter.OnRemoveWall();
        }

		private void tsbShowLights_Click(object sender, EventArgs e)
		{
			mapDrawerControl1.ToggleShowLights();
			tsbShowLights.Checked = !tsbShowLights.Checked;
		}
    }
}
